﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MediaWiki.Office.Model.Utilities;

namespace MediaWiki.Office.Model
{
  public class Shading
  {
    private ContentProperties _parentProperties;
    private Func<Func<Shading, object>, object> _valueGetter;

    private DocumentColor? _color;
    private DocumentColor? _fillColor;
    private string _themeColor;
    private string _themeFillColor;
    private BackgroundPattern? _pattern;

    public BackgroundPattern? Pattern
    {
      get { return (BackgroundPattern?)ValueGetter(s => s._pattern); }
      set { _pattern = value; }
    }

    public string ThemeFillColor
    {
      get { return (string)ValueGetter(s => s._themeFillColor); }
      set { _themeFillColor = value; }
    }

    public string ThemeColor
    {
      get { return (string)ValueGetter(s => s._themeColor); }
      set { _themeColor = value; }
    }

    public DocumentColor? FillColor
    {
      get { return (DocumentColor?)ValueGetter(s => s._fillColor); }
      set { _fillColor = value; }
    }

    public DocumentColor? Color
    {
      get { return (DocumentColor?)ValueGetter(s => s._color); }
      set { _color = value; }
    }

    public Func<Func<Shading, object>, object> ValueGetter
    {
      get { return _valueGetter ?? (g => g(this)); }
    }

    public Shading(ContentProperties parentProperties, XElement shadingElement)
      : this(parentProperties, shadingElement, null)
    {
    }

    public Shading(ContentProperties parentProperties, XElement shadingElement, Func<Func<Shading, object>, object> valueGetter)
    {
      _parentProperties = parentProperties;
      _valueGetter = valueGetter;

      if (shadingElement == null)
        return;

      XNamespace ns = shadingElement.Name.Namespace;

      _color = DocumentColor.Parse((string)shadingElement.Attribute(ns + "color"));
      _fillColor = DocumentColor.Parse((string)shadingElement.Attribute(ns + "fill"));
      _themeColor = (string)shadingElement.Attribute(ns + "themeColor");
      _themeFillColor = (string)shadingElement.Attribute(ns + "themeFill");
      _pattern = XmlUtils.GetValue<BackgroundPattern>(shadingElement, "val");
    }
  }
}
